﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：凭证列表
 */
var companyId = '';
var ztId = '';
var kjnd = '';
var category = '1';
var userId = '';
var userName = '';
var userdefault = {};

var bootstrap = function ($, learun) {
    "use strict";

    var pageWidth = 1024;

    var page = {
        init: function () {
            pageWidth = ($('#gridtable').width());
            var loginInfo = learun.clientdata.get(['userinfo']);
            userdefault = learun.clientdata.get(['usersetting']);

            userId = loginInfo.user.id;
            userName = loginInfo.user.realName;

            page.bind();
        },
        bind: function () {
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);

            //单位性质
            $('#category').lrselect().lrselectSetFirst();
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 执行导入
            $('#lr_excute').on('click', function () {
                if (!$('#form_config').lrValidform()) {
                    return false;
                }
                var productData = $('#gridtable').jfGridGet('rowdatas');
                if (productData.length == 0) {
                    learun.alert.error('表单信息不全,请检查！');
                    return false;
                }
                var postData = productData;

                learun.layerConfirm('注：您确认要导入凭证吗？', function (res, index) {
                    if (res) {
                        var formData = $('#form_config').lrGetFormData();
                        if (formData.kjkmJ == "" || formData.kjkmD == "") {
                            learun.alert.warning('请选择导入的借方科目和贷方科目！');
                            return;
                        }
                        $.lrSaveForm(config.base_server() + 'pz_imports/save?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
                            if (res.resp_code == 200) {
                                top.layer.alert("凭证导入成功");
                            }
                        });

                        top.layer.close(index); //再执行关闭
                    }
                });
            });
            // 从excel导入预览
            $('#lr_import').on('click', function () {
                var formData = $('#form_config').lrGetFormData();
                if (formData.kjkmJ == "" || formData.kjkmD == "") {
                    learun.alert.warning('请选择导入的借方科目和贷方科目！');
                    return;
                }
                category = $("#category").lrselectGet();
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/finance/pz_import/uploadifyForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&category=' + category + '&access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (res) {
                            if (res) {
                                learun.frameTab.currentIframe().$('#companyName').val(res.data.companyName);
                                learun.frameTab.currentIframe().$('#period').val(res.data.period);
                                learun.frameTab.currentIframe().$('#nomalCount').val(res.data.nomalCount);
                                learun.frameTab.currentIframe().$('#errorCount').val(res.data.errorCount);
                                learun.frameTab.currentIframe().$('#errorFileUrl').val(res.data.errorFileUrl);
                                learun.frameTab.currentIframe().$('#errorMsg').val(res.data.errorMsg);
                                learun.frameTab.currentIframe().$('#report1FileUrl').val(res.data.report1FileUrl);
                                learun.frameTab.currentIframe().$('#report2FileUrl').val(res.data.report2FileUrl);
                                learun.frameTab.currentIframe().$('#report3FileUrl').val(res.data.report3FileUrl);
                                learun.frameTab.currentIframe().$('#report4FileUrl').val(res.data.report4FileUrl);
                                learun.frameTab.currentIframe().$('#report5FileUrl').val(res.data.report5FileUrl);
                                learun.frameTab.currentIframe().$('#report6FileUrl').val(res.data.report6FileUrl);
                            }
                        });
                    }
                });
            });

            //从审计导入
            $('#lr_import_xml').on('click', function () {
                var formData = $('#form_config').lrGetFormData();
                if (formData.kjkmJ == "" || formData.kjkmD == "") {
                    learun.alert.warning('请选择导入的借方科目和贷方科目！');
                    return;
                }
                category = $("#category").lrselectGet();
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/finance/pz_import/uploadifyForm2.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&category=' + category + '&access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (res) {
                            if (res) {
                                if(!!res.data&&res.data!='') {
                                    learun.frameTab.currentIframe().$('#errorMsg').val(res.data);
                                }
                                learun.alert.info('导入期初数据成功');
                            }
                        });
                    }
                });
            });

            $('#lr_clear_import_xml').on('click', function () {
                learun.layerConfirm('是否确认删除期初?', function (res) {
                    if (res) {
                        learun.deleteForm(config.base_server() + 'pz_imports/deleteQc?access_token=' + config.getToken().access_token, {}, function () {
                            learun.alert.info('删除期初数据成功');
                        });
                    }
                });
            });

        }
    };


    page.init();
}
